/*
 *   Copyright (C) 2010  Jens Kieselbach
 * 
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 * 
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 * 
 *   You may obtain a copy of the License at http://www.gnu.org/licenses/gpl.html
 */

package de.kieselbach.grunner.dialog

import com.jgoodies.forms.builder.*
import com.jgoodies.forms.layout.*
import de.kieselbach.swing.*
import de.kieselbach.swing.activation.*
import de.kieselbach.swing.table.*
import de.kieselbach.swing.validation.*
import javax.swing.*
import javax.swing.filechooser.*
import java.sql.*
import java.util.logging.*

import java.awt.BorderLayout
import java.awt.Dimension

class SettingsDialog {

    def dialog
    def wasOk = false

    def mainFrame
    def uiResources
    def resourceBundle

    def pathField
    def timeCorrectionField
    def lnfFrameCheckBox
    def lnfDialogCheckBox

    SettingsDialog(def mainframe) {

        mainFrame = mainframe
        uiResources = mainframe.uiResources
        resourceBundle = uiResources.resourceBundles['gui']

        def validator = new Validator()

        pathField = validator.validate(new JTextField(), Validator.EMPTY_TEXT_CHECKER)
        pathField.editable = false
        timeCorrectionField = new JSpinner(new SpinnerNumberModel(0 as int, -23 as int,23 as int, 1 as int))
        lnfFrameCheckBox = new JCheckBox(resourceBundle.getString('settings.lnfframe'))
        lnfDialogCheckBox = new JCheckBox(resourceBundle.getString('settings.lnfdialog'))

        // ------------------------------------------------------------------------------

        def choosepath = new JButton(uiResources.images['open16'])
        choosepath.toolTipText = resourceBundle.getString('settings.choosepath')
        choosepath.preferredSize = [pathField.preferredSize.@height, pathField.preferredSize.@height] as Dimension

        choosepath.actionPerformed = {

            def chooser = new JFileChooser()
            chooser.fileSelectionMode = JFileChooser.DIRECTORIES_ONLY
            chooser.multiSelectionEnabled = false

            if(chooser.showOpenDialog() == JFileChooser.APPROVE_OPTION) {
                def file = chooser.selectedFile
                if(file != null) {
                    pathField.text = file.toString()
                }
            }
        }

        FormLayout layout = new FormLayout("right:pref, 3dlu, fill:210dlu:grow, 3dlu, pref", "p, 3dlu, p, 3dlu, p, 3dlu, p")

        PanelBuilder builder = new PanelBuilder(layout)
        CellConstraints cc = new CellConstraints()

        builder.addLabel(resourceBundle.getString('settings.path') as String, cc.xy(1, 1))
        builder.add(pathField, cc.xy(3, 1))
        builder.add(choosepath, cc.xy(5, 1))

        builder.addLabel(resourceBundle.getString('settings.timecorrection') as String, cc.xy(1, 3))
        builder.add(timeCorrectionField, cc.xy(3, 3))

        builder.add(lnfFrameCheckBox, cc.xy(3, 5))
        builder.add(lnfDialogCheckBox, cc.xy(3, 7))

        def panebuilder = new PaneBuilder()
        dialog = panebuilder.dialog(resourceBundle.getString('settings.title'), owner: mainFrame.frame, modal: true, resizable: false, pack: true, center: true) {
            pane() {
                widget(builder.getPanel())
                bar(type: PaneBuilder.DEFAULT, glue: true)  {
                    ok(default: true, validator: validator, actionPerformed: {
                        wasOk = true
                        dialog.visible = false
                    })
                    filler()
                    cancel(actionPerformed: {
                        wasOk = false
                        dialog.visible = false
                    })
                }
            }
        }
    }

    void show() {
        dialog.setVisible(true)
        dialog.dispose()
    }

    String getPath() {
        pathField.text
    }

    void setPath(String str) {
        pathField.text = str
    }

    int getTimeCorrection() {
        timeCorrectionField.value
    }

    void setTimeCorrection(int val) {
        timeCorrectionField.value = val
    }

    boolean getLnfFrame() {
        lnfFrameCheckBox.selected
    }

    void setLnfFrame(boolean sel) {
        lnfFrameCheckBox.selected = sel
    }

    boolean getLnfDialog() {
        lnfDialogCheckBox.selected
    }

    void setLnfDialog(boolean sel) {
        lnfDialogCheckBox.selected = sel
    }
}

